/**
 * This file is part of LibOSM by Marcus Wolschon <a href="mailto:Marcus@Wolscon.biz">Marcus@Wolscon.biz</a>.
 * You can purchase support for a sensible hourly rate or
 * a commercial license of this file (unless modified by others) by contacting him directly.
 *
 *  LibOSM is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  LibOSM is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with LibOSM.  If not, see <http://www.gnu.org/licenses/>.
 *
 ***********************************
 * Editing this file:
 *  -For consistent code-quality this file should be checked with the
 *   checkstyle-ruleset enclosed in this project.
 *  -After the design of this file has settled it should get it's own
 *   JUnit-Test that shall be executed regularly. It is best to write
 *   the test-case BEFORE writing this class and to run it on every build
 *   as a regression-test.
 */
package org.openstreetmap.osm.io;


import java.io.File;
import java.net.URL;

import org.openstreetmap.osm.data.IDataSet;
import org.openstreetmap.osmosis.core.xml.common.CompressionMethod;
import org.openstreetmap.osmosis.core.xml.v0_6.XmlWriter;

/**
 * This class exists to make loading an <a hef="http://wiki.openstreetmap.org/index.php/Develop">OpenStreetMap</a>-file
 * via program-code easier. It wraps the existing functionality
 * in <a href="http://wiki.openstreetmap.org/index.php/Osmosis">Osmosis</a>.
 * @author <a href="mailto:Marcus@Wolschon.biz">Marcus Wolschon</a>
 */
public class FileWriter {

    /**
     * The filename we are writing to.
     */
    private File myFileName;


    /**
     * @param aFileName The filename we are reading from.
     */
    public FileWriter(final File aFileName) {
        super();
        this.myFileName = aFileName;
    }



    /**
     * @param data the map to write
     */
    public void writeOsm(final IDataSet data) {
        DataSetSource source = new DataSetSource(data);

        CompressionMethod compr = CompressionMethod.None;
        if (myFileName.getName().toLowerCase().endsWith(".gz")) {
            compr = CompressionMethod.GZip;
        } else {
            if (myFileName.getName().toLowerCase().endsWith(".bz2")) {
                compr = CompressionMethod.BZip2;
            }
        }

        XmlWriter task = new XmlWriter(this.myFileName, compr);
        source.setSink(task);
        task.release();
    }

}
